package com.fafu.rbac.service.impl;

import com.fafu.rbac.domain.entity.Permission;
import com.fafu.rbac.domain.entity.Role;
import com.fafu.rbac.mapper.PermissionMapper;
import com.fafu.rbac.mapper.RoleMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fafu.rbac.domain.entity.User;
import com.fafu.rbac.mapper.UserMapper;
import com.fafu.rbac.service.UserService;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public Set<String> getRolePermission(Long id) {
        Set<String> res  = new HashSet<>();
        //获取用户拥有的角色
        List<Role> roleList = roleMapper.selectRoleByUserId(id);
        //遍历每个角色拥有的权限
        for (Role role : roleList) {
            res.add("ROLE_"+role.getCode());
            //获取用户的权限列表
            List<Permission> permissionList = permissionMapper.selectPermissionByRoleId(role.getId());
            for (Permission permission : permissionList) {
                res.add(permission.getCode());
            }
        }

        return res;
    }
}
